//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using System.Xml;

namespace OpenLS.Spreadsheet.UI
{
    public class Preferences : ViewModelBase, IXmlSerializable
    {
        private Preferences()
        {
            
        }
        public static Preferences Retrieved 
    {
        get
        {
            if (!File.Exists(FileName))
                return new Preferences();
            try
            {
                using (Stream stream = File.Open(FileName, FileMode.Open))
                {
                    using (XmlReader reader = new XmlTextReader(stream))
                    {
                        var result = new Preferences();
                        result.ReadXml(reader);
                        return result;
                    }
                }
            }
            catch
            {
                return new Preferences();
            }

            
        }
    }

        public void Save(IEnumerable<SpreadsheetDocument> documents)
        {
            DocumentNames.Clear();
            foreach (var document in documents)
            {
                if (document.FileName == null)
                    continue;
                DocumentNames.Add(document.FileName);
            }
            using (Stream stream = File.Open(FileName, FileMode.Create))
            {
                using (XmlWriter writer = new XmlTextWriter(stream, Encoding.Default))
                {
                    WriteXml(writer);
                }
            }

        }
        static string FileName
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "preferences.xml"); }
        }
	

        private bool tabsVisible;
        public bool TabsVisible
        {
            get { return tabsVisible; }
            set { tabsVisible = value;
            notifyPropertyChanged("TabsVisible");
            }
        }

        private bool openPreviousDocuments;
        public bool OpenPreviousDocuments
        {
            get { return openPreviousDocuments; }
            set { openPreviousDocuments = value;notifyPropertyChanged("OpenPreviousDocuments"); }
        }
        public List<string> DocumentNames = new List<string>();

        private bool documentsSelectorVisible;
        public bool DocumentsSelectorVisible
        {
            get { return documentsSelectorVisible; }
            set { documentsSelectorVisible = value;
                notifyPropertyChanged("DocumentsSelectorVisible");
            }
        }

       
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(XmlReader reader)
        {
            using (var context = new ReadContext(reader, null))
            {
                using (ReadContext c = context.Read("Preferences"))
                {
                    TabsVisible = c.GetBoolean("TabsVisible");
                    DocumentsSelectorVisible = c.GetBoolean("DocumentsSelectorVisible");
                    OpenPreviousDocuments = c.GetBoolean("OpenPreviousDocuments");
                    DocumentNames.Clear();
                    while (c.ReaderLocalName == "Document")
                    {
                        using (ReadContext c2 = c.Read("Document"))
                        {
                            DocumentNames.Add(c2.GetString("FileName"));
                        }
                    }
                }
            }

        }

        public void WriteXml(XmlWriter writer)
        {
            using (var c = new WriteContext("Preferences", null, null, writer, null))
            {
                //using (WriteContext c = context.Write("Preferences"))
                {
                    c.SetBoolean("TabsVisible",TabsVisible);
                    c.SetBoolean("DocumentsSelectorVisible", DocumentsSelectorVisible );
                    c.SetBoolean("OpenPreviousDocuments", OpenPreviousDocuments );
                    foreach (string s in DocumentNames)
                    {
                        using (WriteContext c2 = c.Write("Document"))
                        {
                            c2.SetString("FileName", s); 
                        }
                    }
                }
            }

        }

        #endregion
    }
}
